import { computed, h } from 'vue';
import { Input, Button } from 'ant-design-vue';
import { FolderOpenOutlined } from '@ant-design/icons-vue';
import { bindMapFormSchema } from '/@/utils/common/compUtils';
import { usePermission } from '/@/hooks/web/usePermission';
import { rules } from '/@/utils/helper/validator';
usePermission();
function useFormSchemas(_props, handlers) {
  const mapFormSchema = bindMapFormSchema(
    {
      one: {
        colProps: { xs: 24, sm: 24 },
        itemProps: {
          labelCol: { xs: 24, sm: 2 },
          wrapperCol: { xs: 24, sm: 22 },
        },
      },
      tow: {
        colProps: { xs: 24, sm: 12 },
        itemProps: {
          labelCol: { xs: 24, sm: 4 },
          wrapperCol: { xs: 24, sm: 20 },
        },
      },
      three: {
        colProps: { xs: 24, sm: 8 },
        itemProps: {
          labelCol: { xs: 24, sm: 6 },
          wrapperCol: { xs: 24, sm: 18 },
        },
      },
    },
    'three'
  );
  const formSchemas = [
    { label: '', field: 'id', component: 'Input', show: false },
    { label: '', field: 'tableVersion', component: 'Input', show: false },
    mapFormSchema({
      label: '表名',
      field: 'tableName',
      component: 'Input',
      required: true,
      dynamicDisabled: ({ model }) => model.tableVersion && model.tableVersion != 1,
      //dynamicRules: ({ model, schema }) => rules.duplicateCheckRule('onl_cgform_head', 'table_name', model, schema, true),
    }),
    mapFormSchema({
      label: '表描述',
      field: 'tableTxt',
      component: 'Input',
      required: true,
    }),
    mapFormSchema({
      label: '表类型',
      field: 'tableType',
      component: 'Select',
      defaultValue: 1,
      componentProps: {
        options: [
          { label: '单表', value: 1 },
          { label: '主表', value: 2 },
          { label: '附表', value: 3 },
        ],
        onChange: handlers.onTableTypeChange,
      },
    }),
    {
      label: '',
      field: 'relationType',
      component: 'InputNumber',
      render: () => '',
      colProps: { xs: 0, sm: 17 },
      ifShow: fieldIfShow,
    },
    mapFormSchema({
      label: '',
      field: 'relationType',
      component: 'RadioGroup',
      defaultValue: 0,
      componentProps: {
        options: [
          { label: '一对多', value: 0 },
          { label: '一对一', value: 1 },
        ],
      },
      colProps: { xs: 24, sm: 4 },
      itemProps: {
        colon: false,
        labelCol: { xs: 0, sm: 0 },
        wrapperCol: { xs: 24, sm: 24 },
      },
      ifShow: fieldIfShow,
    }),
    mapFormSchema({
      label: '序号',
      field: 'tabOrderNum',
      component: 'InputNumber',
      componentProps: {
        style: {
          width: '100%',
        },
      },
      colProps: { xs: 24, sm: 3 },
      itemProps: {
        labelCol: { xs: 24, sm: 7 },
        wrapperCol: { xs: 24, sm: 24 - 7 },
      },
      ifShow: fieldIfShow,
    }),
    mapFormSchema({
      label: '表单分类',
      field: 'formCategory',
      component: 'JDictSelectTag',
      defaultValue: 'temp',
      componentProps: {
        dictCode: 'ol_form_biz_type',
      },
    }),
    mapFormSchema({
      label: '主键策略',
      field: 'idType',
      component: 'Select',
      defaultValue: 'UUID',
      componentProps: {
        options: [{ label: 'ID_WORKER(分布式自增)', value: 'UUID' }],
      },
    }),
    mapFormSchema({
      label: '序号名称',
      field: 'idSequence',
      component: 'Input',
      componentProps: {},
      ifShow: fieldIfShow,
    }),
    mapFormSchema({
      label: '显示复选框',
      field: 'isCheckbox',
      component: 'Select',
      defaultValue: 'Y',
      componentProps: {
        options: [
          { label: '是', value: 'Y' },
          { label: '否', value: 'N' },
        ],
      },
    }),
    mapFormSchema({
      label: '主题模板',
      field: 'themeTemplate',
      component: 'Select',
      defaultValue: 'normal',
      componentProps: {
        options: [{ label: '默认主题', value: 'normal' }],
      },
      dynamicDisabled: ({ model }) => model.tableType === 1,
    }),
    mapFormSchema({
      label: '表单风格',
      field: 'formTemplate',
      component: 'Select',
      defaultValue: '1',
      componentProps: {
        options: [
          { label: '一列', value: '1' },
          { label: '两列', value: '2' },
          { label: '三列', value: '3' },
          { label: '四列', value: '4' },
        ],
        placeholder: '请选择PC表单风格',
      },
    }),
    mapFormSchema({
      label: '移动表单风格',
      field: 'formTemplateMobile',
      component: 'Select',
      defaultValue: '1',
      componentProps: {
        options: [
          { label: 'AntDesign模板', value: '1' },
          { label: 'Bootstrap模板', value: '2' },
        ],
        placeholder: '请选择移动表单风格',
      },
      ifShow: false,
    }),
    mapFormSchema({
      label: '滚动条',
      field: 'scroll',
      component: 'Select',
      defaultValue: 1,
      componentProps: {
        options: [
          { label: '有', value: 1 },
          { label: '无', value: 0 },
        ],
      },
    }),
    mapFormSchema({
      label: '是否分页',
      field: 'isPage',
      component: 'Select',
      defaultValue: 'Y',
      componentProps: {
        options: [
          { label: '是', value: 'Y' },
          { label: '否', value: 'N' },
        ],
      },
    }),
    mapFormSchema({
      label: '是否树',
      field: 'isTree',
      component: 'Select',
      defaultValue: 'N',
      componentProps: {
        options: [
          { label: '是', value: 'Y' },
          { label: '否', value: 'N' },
        ],
        onChange: handlers.onIsTreeChange,
      },
      dynamicRules({ model }) {
        return [
          {
            validator({}, value) {
              if (value === 'Y' && (model.tableType == 2 || model.tableType == 3)) {
                return Promise.reject('主表和附表不支持树类型！');
              }
              return Promise.resolve();
            },
          },
        ];
      },
    }),
    mapFormSchema({
      label: ' ',
      field: 'extConfigJson',
      component: 'Input',
      slot: 'extConfigButton',
      itemProps: { colon: false },
    }),
    mapFormSchema({
      label: '树表单父ID',
      field: 'treeParentIdField',
      component: 'Input',
      ifShow: fieldIfShow,
    }),
    mapFormSchema({
      label: '是否有子节点字段',
      field: 'treeIdField',
      component: 'Input',
      show: false,
    }),
    mapFormSchema({
      label: '树开表单列',
      field: 'treeFieldname',
      required: true,
      component: 'Input',
      ifShow: fieldIfShow,
    }),
    mapFormSchema(
      {
        label: '附表',
        field: 'subTableStr',
        component: 'Input',
        componentProps: {
          disabled: true,
          placeholder: ' ',
          allowClear: false,
        },
        ifShow: handlers.ifShowOfSubTableStr,
      },
      'one'
    ),
  ];
  function fieldIfShow({ field, model }) {
    switch (field) {
      case 'relationType':
      case 'tabOrderNum':
        return model.tableType === 3;
      case 'treeParentIdField':
      case 'treeIdField':
      case 'treeFieldname':
        return model.isTree === 'Y';
      case 'idSequence':
        return model.idType === 'SEQUENCE';
    }
    return true;
  }
  return { formSchemas };
}
function useExtendConfigFormSchemas(_props, handlers) {
  const mapFormSchema = bindMapFormSchema(
    {
      left: {
        colProps: { xs: 24, sm: 7 },
        itemProps: {
          labelCol: { xs: 24, sm: 11 },
          wrapperCol: { xs: 24, sm: 13 },
        },
        style: { width: '100%' },
      },
      right: {
        colProps: { xs: 24, sm: 17 },
        itemProps: {
          labelCol: { xs: 24, sm: 3 },
          wrapperCol: { xs: 24, sm: 20 },
        },
        style: { width: '100%' },
      },
    },
    'left'
  );
  const formSchemas = [
    mapFormSchema(
      {
        label: '集成设计表单',
        field: 'isDesForm',
        component: 'RadioButtonGroup',
        componentProps: {
          options: [
            { label: '开启', value: 'Y' },
            { label: '关闭', value: 'N' },
          ],
          buttonStyle: 'solid',
          onChange: handlers.onIsDesformChange,
        },
      },
      'left'
    ),
    mapFormSchema(
      {
        label: '表单编码',
        field: 'desFormCode',
        component: 'Input',
        componentProps: {
          style: 'width: 80%',
        },
        dynamicDisabled: ({ model }) => model.isDesForm !== 'Y',
        dynamicRules: ({ model }) => {
          return [{ required: model.isDesForm === 'Y', message: '请输入表单编码！' }];
        },
      },
      'right'
    ),
    mapFormSchema(
      {
        label: '\u96C6\u6210\u79EF\u6728\u62A5\u8868',
        field: 'reportPrintShow',
        component: 'RadioButtonGroup',
        componentProps: {
          options: [
            { label: '开启', value: 1 },
            { label: '关闭', value: 0 },
          ],
          buttonStyle: 'solid',
          onChange: handlers.onReportPrintShowChange,
        },
      },
      'left'
    ),
    mapFormSchema(
      {
        label: '报表地址',
        field: 'reportPrintUrl',
        component: 'Input',
        componentProps: {
          style: 'width: 80%',
        },
        dynamicDisabled: ({ model }) => !model.reportPrintShow,
        dynamicRules: ({ model }) => {
          return [
            { required: !!model.reportPrintShow, message: '请输入报表地址！' },
            {
              validator({}, value) {
                if (/\/jmreport\/view\/{积木报表ID}/.test(value)) {
                  return Promise.reject('请将{积木报表ID}替换为真实的积木报表ID！');
                }
                return Promise.resolve();
              },
            },
          ];
        },
      },
      'right'
    ),
    mapFormSchema(
      {
        label: '固定操作列',
        field: 'tableFixedAction',
        component: 'RadioButtonGroup',
        componentProps: {
          options: [
            { label: '开启', value: 1 },
            { label: '关闭', value: 0 },
          ],
          buttonStyle: 'solid',
        },
        defaultValue: 0,
      },
      'left'
    ),
    mapFormSchema(
      {
        label: '固定方式',
        field: 'tableFixedActionType',
        component: 'Select',
        componentProps: {
          options: [
            { label: '固定到右侧', value: 'right' },
            { label: '固定到左侧', value: 'left' },
          ],
          style: 'width: 80%',
        },
        defaultValue: 'right',
        dynamicDisabled: ({ model }) => !model.tableFixedAction,
        dynamicRules: ({ model }) => {
          return [{ required: !!model.tableFixedAction, message: '请选择固定方式！' }];
        },
      },
      'right'
    ),
    mapFormSchema(
      {
        label: '\u542F\u7528\u8054\u5408\u67E5\u8BE2',
        field: 'joinQuery',
        component: 'RadioButtonGroup',
        componentProps: {
          options: [
            { label: '开启', value: 1 },
            { label: '关闭', value: 0 },
          ],
          buttonStyle: 'solid',
          onChange: handlers.onJoinQueryChange,
        },
      },
      'left'
    ),
    mapFormSchema(
      {
        label: '',
        field: 'joinQuery',
        component: 'InputNumber',
        render: () => '',
      },
      'right'
    ),
    mapFormSchema(
      {
        label: '弹窗默认全屏',
        field: 'modelFullscreen',
        component: 'RadioButtonGroup',
        componentProps: {
          options: [
            { label: '开启', value: 1 },
            { label: '关闭', value: 0 },
          ],
          buttonStyle: 'solid',
        },
      },
      'left'
    ),
    mapFormSchema(
      {
        label: '弹窗宽度',
        field: 'modalMinWidth',
        component: 'InputNumber',
        componentProps: {
          style: 'width: 80%',
          placeholder: '弹窗最小宽度（单位：px）',
        },
      },
      'right'
    ),
    mapFormSchema(
      {
        label: '开启表单评论',
        field: 'commentStatus',
        component: 'RadioButtonGroup',
        componentProps: {
          options: [
            { label: '开启', value: 1 },
            { label: '关闭', value: 0 },
          ],
          buttonStyle: 'solid',
        },
      },
      'left'
    ),
    mapFormSchema(
      {
        label: '',
        field: 'commentStatus',
        component: 'InputNumber',
        render: () => '',
      },
      'right'
    ),
  ];
  return { formSchemas };
}
function useCodeGeneratorFormSchemas(_, handlers, single) {
  const mapFormSchema = bindMapFormSchema(
    {
      one: {
        colProps: { xs: 24, sm: 24 },
        itemProps: {
          labelCol: { xs: 24, sm: 5 },
          wrapperCol: { xs: 24, sm: 16 },
        },
      },
      towOne: {
        colProps: { xs: 24, sm: 24 },
        itemProps: {
          labelCol: { xs: 24, sm: 3 },
          wrapperCol: { xs: 24, sm: 20 },
        },
      },
      tow: {
        colProps: { xs: 24, sm: 12 },
        itemProps: {
          labelCol: { xs: 24, sm: 6 },
          wrapperCol: { xs: 24, sm: 16 },
        },
      },
    },
    'one'
  );
  const getColSize = computed(() => (single.value ? 'one' : 'tow'));
  const formSchemas = computed(() => [
    mapFormSchema(
      {
        label: '代码生成目录',
        field: 'projectPath',
        render: ({ model, field }) =>
          h(
            Input.Search,
            {
              value: model[field],
              onChange: (e) => {
                model[field] = e.target.value;
                handlers.onProjectPathChange(e);
              },
              onSearch: handlers.onProjectPathSearch,
            },
            {
              enterButton: () =>
                h(
                  Button,
                  {
                    preIcon: 'ant-design:folder-open',
                  },
                  {
                    default: () => '浏览',
                    icon: () => h(FolderOpenOutlined),
                  }
                ),
            }
          ),
        component: 'InputSearch',
        required: true,
      },
      single.value ? 'one' : 'towOne'
    ),
    mapFormSchema(
      {
        label: '页面风格',
        field: 'jspMode',
        component: 'Select',
        componentProps: {
          options: handlers.jspModeOptions.value,
        },
      },
      getColSize.value
    ),
    mapFormSchema(
      {
        label: '功能说明',
        field: 'ftlDescription',
        component: 'Input',
      },
      getColSize.value
    ),
    { label: '数据模型', field: 'jformType', component: 'Input', show: false },
    mapFormSchema(
      {
        label: '表名',
        field: 'tableName_tmp',
        required: true,
        dynamicDisabled: true,
        component: 'Input',
      },
      getColSize.value
    ),
    mapFormSchema(
      {
        label: '实体类名',
        field: 'entityName',
        required: true,
        component: 'Input',
        componentProps: {
          placeholder: '请输入实体类名(首字母大写)',
        },
      },
      getColSize.value
    ),
    mapFormSchema(
      {
        label: '包名(小写)',
        field: 'entityPackage',
        required: true,
        component: 'Input',
      },
      getColSize.value
    ),
    mapFormSchema(
      {
        label: '代码分层样式',
        field: 'packageStyle',
        component: 'Select',
        componentProps: {
          disabled: true,
          options: [
            { label: '业务分层', value: 'service' },
            { label: '代码分层', value: 'project' },
          ],
        },
      },
      getColSize.value
    ),
    mapFormSchema(
      {
        label: '页面代码',
        field: 'vueStyle',
        required: true,
        component: 'RadioGroup',
        defaultValue: 'vue3',
        componentProps: {
          options: [
            { label: 'Vue3', value: 'vue3' },
            { label: 'Vue3 原生', value: 'vue3Native' },
            { label: 'Vue2', value: 'vue' },
          ],
        },
      },
      getColSize.value
    ),
    { label: '需要生成的代码', field: 'codeTypes', component: 'Input', show: false },
  ]);
  return { formSchemas };
}
export { useExtendConfigFormSchemas, useCodeGeneratorFormSchemas, useFormSchemas };
